<?php

namespace app\admin\command;

use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\service\TxEss;
use app\admin\model\Staff;
use app\admin\model\Company;

class Getcontractstatus extends Command
{
    protected function configure()
    {
        $this->setName('getcontractstatus')
             ->setDescription('定时通过接口获取合同状态');
    }

    protected function execute(Input $input, Output $output)
    {

        // 查询公司列表
        $companyList = Company::where('id', '>', 0)->select();
        if ($companyList) {
            foreach ($companyList as $companyItem) {

                $output->writeln('-----查询公司“'.$companyItem['name'].'”------');

                $agent = 0;
                if ($companyItem['name'] == '安徽酷驰人力资源有限公司') {
                    $agent = 1;
                }


                // 分页查询
                $page = 1;
                $pagenum = 100;
                $totalpage = 200;

                for ($page=1; $page <= $totalpage; $page++) {
                    // 查询有flowid的员工列表
                    $stafflist = Staff::where(['company_id'=>$companyItem['id'], 'flowid'=>['<>', ''], 'contract_status'=>['not in', [3,4,5,6,10,21]]])->paginate(      [
                                  'list_rows' => $pagenum, // 每页数量
                                  'page' => $page, // 当前页码
                              ]);
                    $output->writeln('查询有flowid的员工并且contract_status不为3,4,5,6,10,21的列表数量：'.count($stafflist));

                    if (count($stafflist)) {

                        // \think\Log::info('查询有flowid的员工并且contract_status不为3,4,5,6,10,21的列表数量：'.count($stafflist));

                        $flowid_arr = [];
                        foreach ($stafflist as $list) {
                            $flowid_arr[] = $list['flowid'];
                        }

                        $txEss = new TxEss($companyItem['operatorUserId'], $companyItem['serverSignSealId']);
                        $contractInfo = $txEss->detailContract($flowid_arr, $agent);
                        if ($contractInfo['code']) {
                            $output->writeln('“查询合同流程的详情信息”出错：'.$contractInfo['msg']);
                            \think\Log::info('“查询合同流程的详情信息”出错：'.$contractInfo['msg']);
                        } else {
                            // 循环更新
                            foreach ($contractInfo['data']['FlowDetailInfos'] as $key => $list) {
                                $output->writeln('FlowId:'.$list->FlowId.',FlowName:'.$list->FlowName.',FlowStatus:'.$list->FlowStatus);
                                // \think\Log::info('FlowId:'.$list->FlowId.',FlowName:'.$list->FlowName.',FlowStatus:'.$list->FlowStatus);
                                $res = Staff::where('flowid', $list->FlowId)->update(['contract_status'=>$list->FlowStatus, 'updatetime'=>time()]);
                                if (empty($res)) {
                                    $output->writeln('FlowId:'.$list->FlowId.'，更新合同状态为'.$list->FlowStatus.'失败');
                                    \think\Log::info('FlowId:'.$list->FlowId.'，更新合同状态为'.$list->FlowStatus.'失败');
                                } else {
                                    $output->writeln('FlowId:'.$list->FlowId.'，更新合同状态为'.$list->FlowStatus.'成功');
                                    // \think\Log::info('FlowId:'.$list->FlowId.'，更新合同状态为'.$list->FlowStatus.'成功');
                                }
                            }
                        }

                        usleep(100000);
                    } else {
                        $output->writeln('没有数据，退出当前分页查询循环');
                        break;
                    }

                }
            }
        }

        $output->writeln(PHP_EOL.'获取合同状态脚本 执行完毕.');
        // \think\Log::info('获取合同状态脚本 执行完毕.');
    }
}